<?php

class Frontend extends CI_Controller {
	
	// Number of post per page 
	public $paging = 5;
	
	function __construct()
	{
		parent::__construct();	
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('FrontendModel');
		session_start();
	}
	
	function index()
	{
		$tag = null;
		$currentPage = ($this->input->get('current_page'));
		$currentPage = !empty($currentPage) ? $currentPage : 1;
		
		$start = ($currentPage - 1) * $this->paging;
		$data['threads'] = $this->FrontendModel->getThreadsByTag($tag, $this->paging, $start);
		
		// Paging
		$noThreads = count($this->FrontendModel->getNoThreads());
		$data['currentPage'] = $currentPage;
		$data['lastPage'] = floor($noThreads / $this->paging) + 1;
		$data['tag'] = $tag;
		
		$data['mainContent'] = 'frontend_view';

		$this->load->view('master_view', $data);
	}
	
	/**
	 * View a thread in detail
	 * @param int $id - $thread
	 */
	function thread($id)
	{
		if (!empty($id)) {
			// Thread
			$data['thread'] = $this->FrontendModel->getThread($id);
			// Post
			$data['posts'] = $this->FrontendModel->getThreadPosts($id);
			// Tags
			$data['tags'] = $this->FrontendModel->getThreadTags($id); 
		
			$data['mainContent'] = 'thread_view';
			$this->load->view('master_view', $data);
		} else {
			redirect('/', 'refresh');
		}
	}
	
	/**
	 * Create new thread
	 */
	function create()
	{
		if ($_SERVER['REQUEST_METHOD'] == 'GET') {
			// Go to create thread form
			$data['mainContent'] = 'newthread_view';
			$this->load->view('master_view', $data);
		} else {
			$title = $this->input->post('title');
			$content = $this->input->post('content');
			$tags = $this->input->post('tags');
		
			if (!empty($title) && !empty($content)) {
				// Create new thread 
				$this->FrontendModel->createThread($title, $content, $tags);
				// Update tags
				if (!empty($tags)) {
					// Get newest thread id
					$newThreadId = $this->FrontendModel->getNewestThread()->id;
					// Save tags
					$this->FrontendModel->saveTags($newThreadId, $tags);
				}
				redirect('/', 'refresh');
			} else {
				$data['errors'] = array('You must input both title and content');
				
				$data['mainContent'] = 'thread_view';
				$this->load->view('master_view', $data);
			}
		}
	}
	
	/**
	 * Delete a thread
	 * @param int $id
	 */
	function delete($id) 
	{
		$this->FrontendModel->deleteThread($id);
		redirect('/', 'refresh');
	}
	
	function reply()
	{
		$thread_id = $this->input->post('thread_id');
		$content = $this->input->post('content');
		
		if (!empty($thread_id) && !empty($content)) {
			// Create new post
			$this->FrontendModel->createPost($thread_id, $content);
			// Save thread last bump
			$this->FrontendModel->saveBumpDate($thread_id);
			redirect('frontend/thread/'.$thread_id, 'refresh');
			
		} else if (!empty($thread_id)) {
			redirect('frontend/thread/'.$thread_id, 'refresh');
		}
	}
	
	function deletePost($id, $thread_id)
	{
		$this->FrontendModel->deletePost($id);
		redirect('frontend/thread/'.$thread_id, 'refresh');
	}
	
	/**
	 * Get all threads with a specific
	 * 
	 */
	function tag()
	{
		$tag = $this->input->get('tag');
		$currentPage = ($this->input->get('current_page'));
		$currentPage = !empty($currentPage) ? $currentPage : 1;
			
		$start = ($currentPage-1) * $this->paging;
		$data['threads'] = $this->FrontendModel->getThreadsByTag(urldecode($tag), $this->paging, $start);	
		$data['currentPage'] = $currentPage;
		
		$noThreads = count($this->FrontendModel->getNoThreads(urldecode($tag)));
		$data['lastPage'] = floor($noThreads / $this->paging) + 1;
		$data['tag'] = $tag;
		
		$data['mainContent'] = 'frontend_view';
		$this->load->view('master_view', $data);
	}
}